package com.strongdigit.aop.aspectj;

import com.strongdigit.aop.aspectj.biz.MoocMethod;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.SoftException;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import javax.rmi.CORBA.PortableRemoteObjectDelegate;

@Component
@Aspect
public class MoocAspect {

    //标识以Biz结尾的类中的所有方法
    @Pointcut("execution(* com.strongdigit.aop.aspectj.biz.*Biz.*(..))")
    public void pointcut() {
    }


    //within是指这个包内部的类
    @Pointcut("within(com.strongdigit.aop.aspectj.biz.*)")
    public void bizpointcut() {
    }


    @Pointcut("execution(* com.strongdigit.aop.aspectj.biz.MoocBiz.afterThrowing(..))")
    public void throwingcut() {
    }

    //组合切入点表达式
    @Pointcut("pointcut() && bizpointcut()")
    public void zuhebizpointcut() {
    }

    //@Before("execution(* com.strongdigit.aop.aspectj.biz.*Biz.*(..))")
    @Before("pointcut()")
    public void before() {
        System.out.println("before通知执行了");
    }

    //参数的通知获取参数内容
    @Before("pointcut() && args(arg)")
    public void beforeWithArgs(String arg) {

        System.out.println("beforeWithArgs通知执行了,获取的参数为：" + arg);
    }

    //注解的方式使通知获取参数
    @Before("pointcut() && @annotation(moocMethod)")
    public void beforeWithAnnotation(MoocMethod moocMethod) {

        System.out.println("beforeWithAnnotation通知执行了,获取的参数为：" + moocMethod.value());
    }



    @AfterReturning(pointcut = "pointcut()", returning = "retVal")
    public void afterReturning(Object retVal) {

        if (retVal == null) {
            System.out.println("afterReturning连接点返回值为：null");
        } else {
            System.out.println("afterReturning连接点返回值为：" + retVal.toString());
        }

    }

    @AfterThrowing(pointcut = "throwingcut()", throwing = "ex")
    public void afterThrowing(Exception ex) {
        System.out.println("连接点抛出的异常信息为：" + ex.getMessage());
    }

    @After("pointcut()")
    public void after() {
        System.out.println("after执行了");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        System.out.println("around执行proceed前");

        Object obj = pjp.proceed(pjp.getArgs());
        System.out.println("around我是获取的返回值：" + obj.toString());
        System.out.println("around执行proceed后");
        return obj;
    }


}
